(function (window) {
    //dorado.widget.myAttachement={}
    var attachment=function () {
        
    };
    attachment.prototype={
        doInitial:function(arrs){

            var rst=[];
            if(arrs&&arrs instanceof Array&&arrs.length>0){
                    arrs.forEach(function (one) {
                        if(one instanceof Object){
                            var block=this.generateFjBlock(one);
                            rst.push(block);

                        }else{
                            //出现不是json的数据，不能够进行div的创建
                        }
                    });
            }else {
                //不是数组或者数据是空的。
            }
            return rst;
        }
        ,
        /**
         * 用于跟你就json中的数据生成div附件block
         * @param json
         * @returns {jQuery|HTMLElement}
         */
        generateFjBlock:function(json){
            var $div=$('<div>');
            $div.on("download",function (e,v,c) {
                //下载附件只需要重载这个方法就可以
            });
            $div.on("delete",function (e,v,c) {
                //删除附件只需要重载这个方法既可以
            });


            $div.addClass("fj-div-info");

            var $div_c1=$('<div>');
            $div_c1.addClass("m-attachment");
            $div_c1.attr('title',json['title']);

            var $c1_wrapper=$('<div>');
            $c1_wrapper.addClass("wrapper");

            var $wrapper_action=$('<div>')
            $wrapper_action.addClass("action");

            var $action_link=$('<div>')
            $action_link.addClass("m-links");


            var $link_download=$('<span>')
            $link_download.addClass('link');
            $link_download.attr({
                title:'下载'
            });

            $link_download.click(function () {
                $div.trigger('download',json);
            });
            $link_download.text('下载');
            $action_link.append($link_download);


            var $link_del=$('<span>')
            $link_del.addClass('link');
            $link_del.attr({
                title:'删除'
            });

            $link_del.click(function () {
                ///删除附件的方法已经移除
                $div.trigger("delete",json);
                $div.remove();
            });
            $link_del.text('删除');


            $action_link.append($link_del);

            $wrapper_action.append($action_link);


            var $wrapper_icon=$('<div>')
            //注意图标是随着文件类型而变化的
            $wrapper_icon.addClass("attchtrumb w-icon-attach-m "+this.getIcoClass(json['title']));

            var $wrapper_attach=$('<div>')
            $wrapper_attach.addClass("attch");

            var $attach_info=$('<div>');
            $attach_info.addClass('attchinfo');

            var $info_name=$('<span>');
            $info_name.addClass('attchname');
            $info_name.text(json['title']);

            var $info_size=$('<span>');
            $info_size.addClass('attchsize');
            $info_size.text(json['size']);

            $attach_info.append($info_name);
            $attach_info.append($info_size);

            var $attach_status=$('<div>');
            $attach_status.addClass('attchstatus');
            var $status_txt=$('<span>');


            $status_txt.addClass("f-txt-succ");
            $status_txt.text('上传成功');

            $attach_status.append($status_txt);


            $wrapper_attach.append($attach_info);
            $wrapper_attach.append($attach_status);


            $c1_wrapper.append($wrapper_action);
            $c1_wrapper.append($wrapper_icon);
            $c1_wrapper.append($wrapper_attach);

            $div_c1.append($c1_wrapper);

            $div.append($div_c1);


            //注册附件上传的事件
            $div.on("failure",function (e,v,c) {
                $status_txt.attr('class',"f-txt-err");
                $status_txt.text("上传失败");
            });
            $div.on("success",function (e,v,c) {
                $status_txt.attr('class',"f-txt-succ");
                $status_txt.text("上传完成");
            });
            $div.on("progress",function (e,v,c) {
                $status_txt.attr('class',"f-txt-blocking");
                $status_txt.text("上传中..."+v);
            });
            return $div;

        },
        /**
         * 获取得到文件名对应的图标
         * @param fileName
         * @returns {string}
         */
        getIcoClass:function (fileName) {
            return "ico-"+this.recognizeMappedType(fileName)+"-m";
        }
        ,
        /**
         * 输入一个文件名，识别出这个文件名的所属css类
         * @param fileName 可以是文件名，也可以是后缀
         * @returns {*}
         */
        recognizeMappedType:function (fileName) {
            if(undefined!=fileName&&null!=fileName&&fileName.length!=0){
                var idx=fileName.lastIndexOf('.');
                if(idx!=-1){
                    var suffix=fileName.substr(idx+1,fileName.length)
                    if(suffix){
                        var rst=this.typeMapper[suffix];
                        if(rst){
                            return rst;
                        }else{
                            return this.typeMapper.other;
                        }
                    }else{
                        return this.typeMapper.other;
                    }
                }else{
                    var rst=this.typeMapper[fileName];
                    if(rst){
                        return rst;
                    }else{
                        return this.typeMapper.other;
                    }
                }
            }else{
                return this.typeMapper.other;
            }
        }
        ,
        typeMapper:{
            "other":"other",
            "xls":"xls",
            "xlsx":"xlsx",
            "csv":"csv",
            "doc":"doc",
            "docx":"docx",
            "wps":"wps",
            "ppt":"ppt",
            "pptx":"pptx",
            "7z":"7z",
            "rar":"rar",
            "cab":"cab",
            "jar":"jar",
            "arj":"arj",
            "tar":"tar",
            "gz":"gz",
            "tgz":"tgz",
            "zip":"zip",
            "iso":"iso",
            "img":"img",
            "nrg":"nrg",
            "mdf":"mdf",
            "mds":"mds",
            "eml":"eml",
            "htm":"htm",
            "html":"html",
            "shtml":"shtml",
            "xhtml":"xhtml",
            "mht":"mht",
            "asp":"asp",
            "aspx":"aspx",
            "js":"js",
            "css":"css",
            "ini":"ini",
            "inf":"inf",
            "xml":"xml",
            "hlp":"hlp",
            "chm":"chm",
            "exe":"exe",
            "app":"app",
            "com":"com",
            "bat":"bat",
            "cmd":"cmd",
            "ttf":"ttf",
            "fon":"fon",
            "pdf":"pdf",
            "psd":"psd",
            "pdd":"pdd",
            "ps":"ps",
            "ai":"ai",
            "eps":"eps",
            "fla":"fla",
            "swf":"swf",
            "flv":"flv",
            "txt":"txt",
            "rtf":"rtf",
            "reg":"reg",
            "ra":"ra",
            "ram":"ram",
            "rpm":"rpm",
            "rmx":"rmx",
            "rm":"rm",
            "rmvb":"rmvb",
            "wm":"wm",
            "wma":"wma",
            "wmv":"wmv",
            "asf":"asf",
            "wmp":"wmp",
            "mov":"mov",
            "qt":"qt",
            "3gp":"3gp",
            "3gpp":"3gpp",
            "amr":"amr",
            "avi":"avi",
            "mkv":"mkv",
            "mp4":"mp4",
            "mpg":"mpg",
            "mpeg":"mpeg",
            "vob":"vob",
            "dat":"dat",
            "ts":"ts",
            "tp":"tp",
            "m2ts":"m2ts",
            "evo":"evo",
            "pmp":"pmp",
            "vp6":"vp6",
            "ivf":"ivf",
            "dsm":"dsm",
            "dsv":"dsv",
            "dsa":"dsa",
            "dss":"dss",
            "fli":"fli",
            "flc":"flc",
            "flic":"flic",
            "roq":"roq",
            "mpa":"mpa",
            "m1a":"m1a",
            "m2a":"m2a",
            "mp2":"mp2",
            "mp3":"mp3",
            "ac3":"ac3",
            "dts":"dts",
            "ddp":"ddp",
            "flac":"flac",
            "ape":"ape",
            "mac":"mac",
            "apl":"apl",
            "shn":"shn",
            "tta":"tta",
            "wv":"wv",
            "cda":"cda",
            "wav":"wav",
            "aac":"aac",
            "m4a":"m4a",
            "mka":"mka",
            "ogg":"ogg",
            "mpc":"mpc",
            "mp":"mp",
            "mpp":"mpp",
            "au":"au",
            "aif":"aif",
            "aifc":"aifc",
            "aiff":"aiff",
            "mid":"mid",
            "midi":"midi",
            "rmi":"rmi",
            "jpg":"jpg",
            "jpeg":"jpeg",
            "gif":"gif",
            "png":"png",
            "bmp":"bmp",
            "dib":"dib",
            "rle":"rle",
            "tif":"tif",
            "tiff":"tiff",
            "xif":"xif",
            "emf":"emf",
            "wmf":"wmf",
            "icl":"icl",
            "cur":"cur",
            "ani":"ani",
            "raw":"raw",
            "dll":"dll",
            "manifest":"manifest",
            "bin":"bin",
            "torrent":"torrent",
            "bt":"torrent"
        }
    }
    window.FJ=new attachment();
})(window);



